package com.pureut.storage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pureut.storage.domain.PickingSchemeRelation;
import com.pureut.storage.mapper.PickingSchemeRelationMapper;
import com.pureut.storage.service.PickingSchemeRelationService;
import com.sale.common.core.constant.CacheNames;
import com.sale.common.mybatis.core.page.PageQuery;
import com.sale.common.mybatis.core.page.TableDataInfo;
import com.sale.common.redis.utils.CacheUtils;
import com.sale.system.api.domain.SysDictData;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/***
 * 关联信息实现层
 * Author:C
 * Date:2022/12/21 15:45
 * @DESC
 */
@Service
@RequiredArgsConstructor
public class PickingSchemeRelationServiceImpl implements PickingSchemeRelationService {

    private final PickingSchemeRelationMapper pickingSchemeRelationMapper;

    /**
     * 关联信息列表
     * @param id
     * @param pageQuery
     * @return
     */
    @Override
    public TableDataInfo<PickingSchemeRelation> getRelationList(Long id, PageQuery pageQuery) {

        List<SysDictData> choiceArray = CacheUtils.get(CacheNames.SYS_DICT, "picking_strategy_choice");
        Map<String, String> choiceMap = choiceArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        //管控类型
        List<SysDictData> typeArray = CacheUtils.get(CacheNames.SYS_DICT, "control_type");
        Map<String, String> typeMap = typeArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

//        List<PickingSchemeRelation> pickingSchemeRelations = pickingSchemeRelationMapper.selectList(new QueryWrapper<PickingSchemeRelation>()
//            .lambda().eq(PickingSchemeRelation::getPickingId, id));
        Page<PickingSchemeRelation> pickingSchemeRelations = pickingSchemeRelationMapper.selectPickingSchemeRelation(id, pageQuery.build());
        List<PickingSchemeRelation> records = pickingSchemeRelations.getRecords();
        for (PickingSchemeRelation entity : records) {
            if ("1".equals(entity.getControlType())) {
               entity.setAssociatedDetails(choiceMap.get(entity.getAssociatedDetails()));
            }
            //管控类型
            entity.setControlType(typeMap.get(entity.getControlType()));
        }
//        Page<PickingSchemeRelation> page = new Page<>();
//        Page<PickingSchemeRelation> pickingSchemeRelationPage = page.setRecords(pickingSchemeRelations);
        return TableDataInfo.build(pickingSchemeRelations);
    }
}
